﻿Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Math
''' <summary>
''' 高空气象数据类
''' </summary>
''' <remarks></remarks>
<Serializable()> Public Class UpMetData
    Implements ICloneable

    Private m_FileUpperAirCommunication As New FileUpperAirCommunication
    Private m_reconnaissance As New ReconnaissanceDataConcourse
    Private m_title As New ReconnaissanceTitleData
    Private m_type As Boolean = False
    Property Type() As Boolean
        Get
            Return Me.m_type

        End Get
        Set(ByVal value As Boolean)
            Me.m_type = value
        End Set
    End Property
    ''' <summary>
    ''' 高空气象数据的标题类
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property title() As ReconnaissanceTitleData
        Get
            Return m_title
        End Get
        Set(ByVal value As ReconnaissanceTitleData)
            m_title = value
        End Set
    End Property
    Property FileUpperAirCommunication() As FileUpperAirCommunication
        Get
            Return Me.m_FileUpperAirCommunication
        End Get
        Set(ByVal value As FileUpperAirCommunication)
            Me.m_FileUpperAirCommunication = value
        End Set
    End Property
    ''' <summary>
    ''' 高空气象数据储存数据的类
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property reconnaissance() As ReconnaissanceDataConcourse
        Get
            Return m_reconnaissance
        End Get
        Set(ByVal value As ReconnaissanceDataConcourse)
            m_reconnaissance = value
        End Set
    End Property
    '打开UA文件
    Public Sub OpenFile6201(ByVal str As String)
        Dim L As Integer = 0


        Dim File As String = str

        FileOpen(1, File, OpenMode.Input)
        Try
            If File <> "" Then
                Do Until EOF(1)

                    Dim strline As String = LineInput(1)
                    Dim strYear As String = Mid(strline, 20, 4)

                    If strYear.Length < 4 Then
                        If CInt(strYear) < 10 Then '50年代以前的数据均为2000年
                            strYear = "200" & CInt(strYear)
                        ElseIf CInt(strYear) <= 50 Then
                            strYear = "20" & CInt(strYear)
                        Else
                            strYear = "19" & CInt(strYear)
                        End If
                    End If



                    'ReDim Preserve Me.ArrStructureUpAir(L)
                    'Me.ArrStructureUpAir(L) = New StructureUpAir

                    ReDim Preserve Me.FileUpperAirCommunication.ArrStructureUpAir(L)
                    Me.FileUpperAirCommunication.ArrStructureUpAir(L) = New StructureUpAir

                    Me.FileUpperAirCommunication.UpperAirStations.FileNameIn = Me.GetFileName(File)
                    Me.FileUpperAirCommunication.UpperAirStations.FilePathIn = File
                    '站点号
                    Me.FileUpperAirCommunication.UpperAirStations.StationID = Mid(strline, 1, 8)
                    '纬度
                    Me.FileUpperAirCommunication.UpperAirStations.Latitude = Mid(strline, 9, 4) / 100
                    '纬度代码
                    Me.FileUpperAirCommunication.UpperAirStations.LatitudeCode = Mid(strline, 13, 1)
                    '经度
                    Me.FileUpperAirCommunication.UpperAirStations.Longitude = Mid(strline, 14, 5) / 100
                    '经度代码
                    Me.FileUpperAirCommunication.UpperAirStations.LongitudeCode = Mid(strline, 19, 1)

                    '赋值经纬度给title
                    If L = 0 Then
                        Me.title.Latitude = Me.FileUpperAirCommunication.UpperAirStations.Latitude.ToString + Me.FileUpperAirCommunication.UpperAirStations.LatitudeCode
                        Me.title.Longitude = Me.FileUpperAirCommunication.UpperAirStations.Longitude.ToString + Me.FileUpperAirCommunication.UpperAirStations.LongitudeCode
                    End If
                  



                    Dim cc As Integer = 0
                    '层数据时间
                    Me.FileUpperAirCommunication.ArrStructureUpAir(L).MyDateTime = New DateTime(CInt(strYear), CInt(Mid(strline, 24, 2)), CInt(Mid(strline, 26, 2)), CInt(Mid(strline, 28, 2)), 0, 0) '读取时间
                    '层数据 观测层数
                    Dim kk As Integer = (strline.Length - 33) / 36

                    Me.FileUpperAirCommunication.ArrStructureUpAir(L).TierCount = kk 'Mid(strline, 30, 3)


                    For j As Integer = 0 To Me.FileUpperAirCommunication.ArrStructureUpAir(L).TierCount - 1

                        ReDim Preserve Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j)
                        Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j) = New StructureUpAirTierData


                        'If j = 0 Then

                        'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).DataNameplate = Mid(strline, 33, 1)
                        'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).TimeRelease = Mid(strline, 34, 4)

                        'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).Airpressure = Mid(strline, 38, 5)
                        'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).Height = Mid(strline, 43, 6)
                        'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).AirTemperature = Mid(strline, 49, 4)
                        'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).Humidity = Mid(strline, 53, 3)

                        'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).WindVane = Mid(strline, 56, 3)
                        'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).WindSpeed = Mid(strline, 59, 3)
                        'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).QualityFlags = Mid(strline, 62, 6)
                        'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).TierMark = Mid(strline, 68, 1)

                        'Else

                        If (36 * j + 33 < strline.Length) Then
                            '数据标示 (Integer)
                            Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).DataNameplate = Mid(strline, 36 * j + 33, 1)
                            '释放时间
                            Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).TimeRelease = Mid(strline, 36 * j + 34, 4)
                            '气压
                            Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).Airpressure = Mid(strline, 36 * j + 38, 5)
                            '高度
                            Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).Height = Mid(strline, 36 * j + 43, 6)
                            '空气温度
                            Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).AirTemperature = Mid(strline, 36 * j + 49, 4)
                            '相对湿度
                            Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).Humidity = Mid(strline, 36 * j + 53, 3)
                            '风向
                            Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).WindVane = Mid(strline, 36 * j + 56, 3)
                            '风速
                            Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).WindSpeed = Mid(strline, 36 * j + 59, 3)
                            '质量标示
                            Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).QualityFlags = Mid(strline, 36 * j + 62, 6)

                            Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(j).TierMark = Mid(strline, 36 * j + 68, 1)

                        End If
                    Next


                    'ReDim Preserve Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData.Length - 1)
                    'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData.Length - 1) = New StructureUpAirTierData

                    'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData.Length - 1).DataNameplate = Mid(strline, 33, 1)
                    'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData.Length - 1).TimeRelease = Mid(strline, 34, 4)

                    'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData.Length - 1).Airpressure = Mid(strline, 38, 5)
                    'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData.Length - 1).Height = Mid(strline, 43, 6)
                    'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData.Length - 1).AirTemperature = Mid(strline, 49, 4)
                    'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData.Length - 1).Humidity = Mid(strline, 53, 3)

                    'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData.Length - 1).WindVane = Mid(strline, 56, 3)
                    'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData.Length - 1).WindSpeed = Mid(strline, 59, 3)
                    'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData.Length - 1).QualityFlags = Mid(strline, 62, 6)
                    'Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData(Me.FileUpperAirCommunication.ArrStructureUpAir(L).ArrStructureUpAirTierData.Length - 1).TierMark = Mid(strline, 68, 1)


                    L += 1
                Loop

            End If
        Catch ex As Exception
            Dim a As Integer = 0
        End Try
        FileClose(1)

    End Sub
    Public Sub ReturnOQA(ByVal FileName As String)
        Dim swr As StreamWriter

        Dim month As String = ""
        Dim day As String = ""
        Dim hour As String = ""
        Dim pa As String = ""
        Dim pa1 As String = ""

        Dim DistanceTerraHeight As String = ""
        Dim DistanceTerraHeight1 As String = ""

        Dim DryTemperature As String = ""
        Dim DryTemperature1 As String = ""

        Dim DewPointTemperature As String = ""
        Dim DewPointTemperature1 As String = ""

        Dim WindVane As String = ""
        Dim WindVane1 As String = ""

        Dim WindSpeed As String = ""
        Dim WindSpeed1 As String = ""


        Dim str As String = ""
        Dim rdate As String = ""
        swr = New StreamWriter(FileName)
        swr.WriteLine("*% UPPERAIR                                                                        ")
        Dim six As String = ""
        ''-----------------------------------------------------------------
        ''在这里把经纬度的小数点位数改为2位
        ' ------------------------------------------------------------------
        'Dim Lat As String = FormatNumber(Me.FileUpperAirCommunication.UpperAirStations.Latitude, 2).ToString & Me.FileUpperAirCommunication.UpperAirStations.LatitudeCode  ' Me.m_title.Latitude.Remove(Me.m_title.Latitude.Length - 1, 1) '取得经度
        'Dim Lon As String = FormatNumber(Me.FileUpperAirCommunication.UpperAirStations.Longitude, 2).ToString & Me.FileUpperAirCommunication.UpperAirStations.LongitudeCode ' Me.FileUpperAirCommunication.UpperAirStations.Longitude ' Me.m_title.Longitude.Remove(Me.m_title.Longitude.Length - 1, 1) '取得纬度
        Dim Lat As String = FormatNumber(Me.FileUpperAirCommunication.UpperAirStations.Latitude, 2).ToString & Me.FileUpperAirCommunication.UpperAirStations.LatitudeCode  ' Me.m_title.Latitude.Remove(Me.m_title.Latitude.Length - 1, 1) '取得经度
        Dim Lon As String = FormatNumber(Me.FileUpperAirCommunication.UpperAirStations.Longitude, 2).ToString & Me.FileUpperAirCommunication.UpperAirStations.LongitudeCode ' Me.FileUpperAirCommunication.UpperAirStations.Longitude ' Me.m_title.Longitude.Remove(Me.m_title.Longitude.Length - 1, 1) '取得纬度

        'Me.FileUpperAirCommunication.UpperAirStations.StationID = Mid(strline, 1, 8)
        'Me.FileUpperAirCommunication.UpperAirStations.Latitude = Mid(strline, 9, 4)
        'Me.FileUpperAirCommunication.UpperAirStations.LatitudeCode = Mid(strline, 13, 1)
        'Me.FileUpperAirCommunication.UpperAirStations.Longitude = Mid(strline, 14, 5)
        'Me.FileUpperAirCommunication.UpperAirStations.LongitudeCode = Mid(strline, 19, 1)

        'Me.m_title.Latitude = FormatNumber(Lat, 2).ToString & Me.m_title.Latitude.Remove(0, Me.m_title.Latitude.Length - 1)
        'Me.m_title.Longitude = FormatNumber(Lon, 2).ToString & Me.m_title.Longitude.Remove(0, Me.m_title.Longitude.Length - 1)

        six = Me.FileUpperAirCommunication.UpperAirStations.StationID.PadLeft(10) & Lat.PadLeft(8) & Lon.PadLeft(8) ' & Me.m_title.TimeZone.ToString.PadLeft(3) & Me.m_title.TerraHeight.ToString.PadLeft(6)

        swr.WriteLine("*@     LOCATION" & six)
        swr.WriteLine("*** EOH: END OF UPPERAIR QA HEADERS")
        For f1 As Integer = 0 To Me.FileUpperAirCommunication.ArrStructureUpAir.Length - 1 ' Me.m_ reconnaissance.CountWeather - 1 ' Me.m_ reconnaissance.ArrReconnaissance.Length - 1
            Dim myear As String = ""
            rdate = ""
           
            myear = Me.FileUpperAirCommunication.ArrStructureUpAir(f1).MyDateTime.Year
            rdate += Mid(myear, 3, 2)
            If Me.FileUpperAirCommunication.ArrStructureUpAir(f1).MyDateTime.Month.ToString.Length < 2 Then
                month = "0" & Me.FileUpperAirCommunication.ArrStructureUpAir(f1).MyDateTime.Month
            Else
                month = Me.FileUpperAirCommunication.ArrStructureUpAir(f1).MyDateTime.Month
            End If

            If Me.FileUpperAirCommunication.ArrStructureUpAir(f1).MyDateTime.Day.ToString.Length < 2 Then
                day = "0" & Me.FileUpperAirCommunication.ArrStructureUpAir(f1).MyDateTime.Day
            Else
                day = Me.FileUpperAirCommunication.ArrStructureUpAir(f1).MyDateTime.Day
            End If

            If Me.FileUpperAirCommunication.ArrStructureUpAir(f1).MyDateTime.Hour.ToString.Length < 2 Then
                hour = "0" & Me.FileUpperAirCommunication.ArrStructureUpAir(f1).MyDateTime.Hour
            Else
                hour = Me.FileUpperAirCommunication.ArrStructureUpAir(f1).MyDateTime.Hour
            End If
            rdate = rdate & month & day & hour
            'rdate += 
            'rdate += 
            If rdate = Nothing Then
                rdate = ""
            End If
            rdate = rdate.PadLeft(9)
            rdate += Me.FileUpperAirCommunication.ArrStructureUpAir(f1).TierCount.ToString.PadLeft(5)
            swr.WriteLine(rdate)
            rdate = ""
            If Me.FileUpperAirCommunication.ArrStructureUpAir(f1).TierCount > 0 Then
                For f2 As Integer = 0 To Me.FileUpperAirCommunication.ArrStructureUpAir(f1).ArrStructureUpAirTierData.Length - 1
                    Str = ""
                    If Me.FileUpperAirCommunication.ArrStructureUpAir(f1).ArrStructureUpAirTierData(f2).Airpressure = 0 Then
                        pa1 = 99999
                        pa = pa1.ToString.PadLeft(7)
                    Else
                        pa = Me.FileUpperAirCommunication.ArrStructureUpAir(f1).ArrStructureUpAirTierData(f2).Airpressure.ToString.PadLeft(7)
                    End If
                    If Me.FileUpperAirCommunication.ArrStructureUpAir(f1).ArrStructureUpAirTierData(f2).Height = Nothing Then
                        DistanceTerraHeight1 = -99999
                        DistanceTerraHeight = DistanceTerraHeight1.PadLeft(7)
                    Else
                        DistanceTerraHeight = Me.FileUpperAirCommunication.ArrStructureUpAir(f1).ArrStructureUpAirTierData(f2).Height  '.PadLeft(7)
                        DistanceTerraHeight = DistanceTerraHeight.PadLeft(7)
                    End If
                    If Me.FileUpperAirCommunication.ArrStructureUpAir(f1).ArrStructureUpAirTierData(f2).AirTemperature = Nothing Then
                        DryTemperature1 = -9990
                        DryTemperature = DryTemperature1.PadLeft(7)
                    Else
                        DryTemperature = Me.FileUpperAirCommunication.ArrStructureUpAir(f1).ArrStructureUpAirTierData(f2).AirTemperature '.PadLeft(7)
                        DryTemperature = DryTemperature.PadLeft(7)
                    End If

                    'If Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DewPointTemperature = Nothing Then
                    '    DewPointTemperature1 = -9990
                    '    DewPointTemperature = DewPointTemperature1.PadLeft(7)
                    'Else
                    '    DewPointTemperature = Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DewPointTemperature '.PadLeft(7)
                    '    DewPointTemperature = DewPointTemperature.PadLeft(7)
                    'End If
                    DewPointTemperature = " " & -9990 'td6201 没有露点温度
                    If Me.FileUpperAirCommunication.ArrStructureUpAir(f1).ArrStructureUpAirTierData(f2).WindVane = Nothing Then
                        '小杨改
                        'WindVane1 = 999
                        WindVane1 = 0
                        WindVane = WindVane1.PadLeft(7)
                    Else
                        WindVane = Me.FileUpperAirCommunication.ArrStructureUpAir(f1).ArrStructureUpAirTierData(f2).WindVane
                        WindVane = WindVane.PadLeft(7)
                    End If
                    If Me.FileUpperAirCommunication.ArrStructureUpAir(f1).ArrStructureUpAirTierData(f2).WindSpeed = Nothing Then
                        '小杨改
                        'WindSpeed1 = 9990
                        WindSpeed1 = 0
                        WindSpeed = FormatNumber(WindSpeed1, 0)
                        WindSpeed = WindSpeed1.PadLeft(7)

                    Else
                        WindSpeed = Me.FileUpperAirCommunication.ArrStructureUpAir(f1).ArrStructureUpAirTierData(f2).WindSpeed
                        WindSpeed = FormatNumber(WindSpeed, 0)
                        WindSpeed = WindSpeed.PadLeft(7)

                    End If
                    Str += pa
                    Str += DistanceTerraHeight
                    Str += DryTemperature
                    Str += DewPointTemperature
                    Str += WindVane
                    Str += WindSpeed
                    swr.WriteLine(Str)
                Next
            Else

            End If

        Next
        swr.Close()
        swr.Dispose()
    End Sub
    ''' <summary>
    ''' 导出高空数据
    ''' </summary>
    ''' <param name="FileName"></param>
    ''' <remarks></remarks>
    Public Sub ExportData(ByVal FileName As String)
        Dim swr As StreamWriter


        Dim pa As String = ""
        Dim pa1 As String = ""

        Dim DistanceTerraHeight As String = ""
        Dim DistanceTerraHeight1 As String = ""

        Dim DryTemperature As String = ""
        Dim DryTemperature1 As String = ""

        Dim DewPointTemperature As String = ""
        Dim DewPointTemperature1 As String = ""

        Dim WindVane As String = ""
        Dim WindVane1 As String = ""

        Dim WindSpeed As String = ""
        Dim WindSpeed1 As String = ""


        Dim str As String = ""
        Dim rdate As String = ""
        swr = New StreamWriter(FileName)



        swr.WriteLine("*% UPPERAIR                                                                        ")
        Dim six As String = ""
        ''-----------------------------------------------------------------
        ''在这里把经纬度的小数点位数改为2位
        ' ------------------------------------------------------------------
        Dim Lat As Double = Me.m_title.Latitude.Remove(Me.m_title.Latitude.Length - 1, 1) '取得经度
        Dim Lon As Double = Me.m_title.Longitude.Remove(Me.m_title.Longitude.Length - 1, 1) '取得纬度

        Me.m_title.Latitude = FormatNumber(Lat, 2).ToString & Me.m_title.Latitude.Remove(0, Me.m_title.Latitude.Length - 1)
        Me.m_title.Longitude = FormatNumber(Lon, 2).ToString & Me.m_title.Longitude.Remove(0, Me.m_title.Longitude.Length - 1)

        six = Me.m_title.WeatherID.PadLeft(10) & Me.m_title.Latitude.PadLeft(8) & Me.m_title.Longitude.PadLeft(8) & Me.m_title.TimeZone.ToString.PadLeft(3) & Me.m_title.TerraHeight.ToString.PadLeft(6)

        swr.WriteLine("*@     LOCATION" & six)
        swr.WriteLine("*** EOH: END OF UPPERAIR QA HEADERS")
        For f1 As Integer = 0 To Me.m_reconnaissance.ArrReconnaissance.Length - 1 ' Me.m_ reconnaissance.CountWeather - 1 ' Me.m_ reconnaissance.ArrReconnaissance.Length - 1
            Dim myear As String = ""
            rdate = ""
            myear = Me.m_reconnaissance.ArrReconnaissance(f1).Year
            rdate += Mid(myear, 3, 2)
            rdate += Me.m_reconnaissance.ArrReconnaissance(f1).Month
            rdate += Me.m_reconnaissance.ArrReconnaissance(f1).Day
            rdate += (Int(Me.m_reconnaissance.ArrReconnaissance(f1).Time + Me.title.TimeModify)).ToString.PadLeft(2, "0")
            If rdate = Nothing Then
                rdate = ""
            End If
            rdate = rdate.PadLeft(9)
            rdate += Me.m_reconnaissance.ArrReconnaissance(f1).TierCount.ToString.PadLeft(5)
            swr.WriteLine(rdate)
            rdate = ""
            If Me.m_reconnaissance.ArrReconnaissance(f1).TierCount > 0 Then
                For f2 As Integer = 0 To Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData.Length - 1
                    str = ""
                    If Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).Pa.ToString = 0 Then
                        pa1 = 99999
                        pa = pa1.ToString.PadLeft(7)
                    Else
                        pa = Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).Pa.ToString.PadLeft(7)
                    End If
                    If Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DistanceTerraHeight = Nothing Then
                        DistanceTerraHeight1 = -99999
                        DistanceTerraHeight = DistanceTerraHeight1.PadLeft(7)
                    Else
                        DistanceTerraHeight = Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DistanceTerraHeight '.PadLeft(7)
                        DistanceTerraHeight = DistanceTerraHeight.PadLeft(7)
                    End If
                    If Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature = Nothing Then
                        DryTemperature1 = -9990
                        DryTemperature = DryTemperature1.PadLeft(7)
                    Else
                        DryTemperature = Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature '.PadLeft(7)
                        DryTemperature = DryTemperature.PadLeft(7)
                    End If

                    If Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DewPointTemperature = Nothing Then
                        DewPointTemperature1 = -9990
                        DewPointTemperature = DewPointTemperature1.PadLeft(7)
                    Else
                        DewPointTemperature = Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DewPointTemperature '.PadLeft(7)
                        DewPointTemperature = DewPointTemperature.PadLeft(7)
                    End If
                    If Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindVane = Nothing Then
                        WindVane1 = 999
                        WindVane = WindVane1.PadLeft(7)
                    Else
                        WindVane = Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindVane
                        WindVane = WindVane.PadLeft(7)
                    End If
                    If Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindSpeed = Nothing Then
                        WindSpeed1 = 9990
                        WindSpeed1 = FormatNumber(WindSpeed1, 0)
                        WindSpeed = WindSpeed1.PadLeft(7)
                    Else
                        WindSpeed = Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindSpeed
                        WindSpeed = FormatNumber(WindSpeed, 0)
                        WindSpeed = WindSpeed.PadLeft(7)

                    End If
                    str += pa
                    str += DistanceTerraHeight
                    str += DryTemperature
                    str += DewPointTemperature
                    str += WindVane
                    str += WindSpeed
                    swr.WriteLine(str)
                Next
            Else

            End If

        Next
        swr.Close()
        swr.Dispose()
      
    End Sub

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim objCopy As New UpMetData()
        objCopy.m_reconnaissance = Me.m_reconnaissance.Clone
        objCopy.FileUpperAirCommunication = Me.m_FileUpperAirCommunication.Clone
        objCopy.m_title = Me.m_title.Clone
        objCopy.m_type = Me.m_type
        Return objCopy

    End Function

    <OnDeserializing()> _
 Friend Sub OnDeserializingMethod(ByVal context As StreamingContext)

    End Sub

    <OnDeserialized()> _
    Friend Sub OnDeserializedMethod(ByVal context As StreamingContext)
        If Me.m_reconnaissance Is Nothing Then
            Me.m_reconnaissance = New ReconnaissanceDataConcourse
        End If

        If Me.m_title Is Nothing Then
            Me.m_title = New ReconnaissanceTitleData
        End If

    End Sub
    ''' <summary>
    ''' 删除经纬度 中的小数点
    ''' </summary>
    ''' <param name="str"></param>
    ''' <remarks></remarks>
    Private Function CharIndex(ByVal str As String, ByVal i As Integer) As String
        Dim strIndex As Integer = -1
        strIndex = str.IndexOf(".")
        If strIndex <> -1 Then
            str = str.Remove(strIndex, 1)
        Else
            str = str
        End If
        If str.Length < i Then
            str = str.ToString.PadLeft(i, "0")
        ElseIf str.Length > i Then
            str = Mid(str, 1, i)
        Else
            str = str
        End If
        Return str
    End Function
    Public Sub ReturnTD6201(ByVal FileName As String)
        Dim pa As String = ""
        Dim pa1 As String = ""

        Dim DistanceTerraHeight As String = ""
        Dim DistanceTerraHeight1 As String = ""

        Dim DryTemperature As String = ""
        Dim DryTemperature1 As String = ""

        'Dim DewPointTemperature As String = ""
        'Dim DewPointTemperature1 As String = ""

        Dim WindVane As String = ""
        Dim WindVane1 As String = ""

        Dim WindSpeed As String = ""
        Dim intwindspeed As Integer
        Dim WindSpeed1 As String = ""

        Dim sngRH As String

        Dim strIndex As Integer = -1
        Dim str As String = ""
        Dim rdate As String = ""


        '小阳新加入代码
        Me.title.WeatherID = 51888



        Dim TIME_SINCE_RELEASE As String = "9999" '小阳该"9999" 为"09999"
        'Dim str As String = ""
        Dim myear As String = ""
        Dim swr As StreamWriter
        swr = New StreamWriter(FileName)
        Dim Lat As String = Me.title.Latitude.Remove(Me.title.Latitude.Length - 1, 1) '取得经度
        Dim Lon As String = Me.title.Longitude.Remove(Me.title.Longitude.Length - 1, 1) '取得纬度
        For f1 As Integer = 0 To Me.reconnaissance.ArrReconnaissance.Length - 1


            If Me.reconnaissance.ArrReconnaissance(f1).TierCount > 0 Then
                If Me.reconnaissance.ArrReconnaissance(f1).Year < 50 Then
                    myear = Me.reconnaissance.ArrReconnaissance(f1).Year '"20" &
                Else
                    myear = Me.reconnaissance.ArrReconnaissance(f1).Year '"19"&
                End If

                '经纬度变换
                Lat = Me.CharIndex(Lat, 4)
                ' Lat = Lat.ToString.PadLeft(4, "0")
                Lon = Me.CharIndex(Lon, 5)
                ' Lon = Lon.ToString.PadLeft(5, "0")
                '年
                myear = myear & Me.reconnaissance.ArrReconnaissance(f1).Month & Me.reconnaissance.ArrReconnaissance(f1).Day & Me.reconnaissance.ArrReconnaissance(f1).Time
                '写入每一时次的控制信息
                str = Me.title.WeatherID.PadLeft(8, "0") & Lat.PadLeft(4, "0") & Me.title.Latitude.Remove(0, Me.title.Latitude.Length - 1) _
                & Lon.PadLeft(5, "0") & Me.title.Longitude.Remove(0, Me.title.Longitude.Length - 1) & myear & Me.reconnaissance.ArrReconnaissance(f1).TierCount.ToString.PadLeft(3, "0") '& "0" '& "9"
                swr.Write(str) '写入每一时次的控制信息,不换行
                For f2 As Integer = 0 To Me.reconnaissance.ArrReconnaissance(f1).ArrtierData.Length - 1


                    '气压，单位十帕，5位
                    If Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).Pa.ToString = 0 Then
                        '+1
                        pa1 = 99999
                        'pa = pa1.ToString.PadLeft(5)
                    Else
                        pa = Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).Pa.ToString.PadLeft(5, "0")
                    End If
                    '处理每层位势(海拔)高度，单位m，缺失写入-99999，6位
                    If Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DistanceTerraHeight = Nothing Then
                        '-1
                        DistanceTerraHeight1 = -99999
                        ' DistanceTerraHeight = DistanceTerraHeight1.PadLeft(6)
                    ElseIf Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DistanceTerraHeight = -99999 Then
                        DistanceTerraHeight1 = -99999
                    Else
                        DistanceTerraHeight = Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DistanceTerraHeight + title.TerraHeight  ' td6201 的每层位势高度（海拔）=离地高度+地面海拔.PadLeft(7)
                        DistanceTerraHeight = CInt(DistanceTerraHeight)
                        DistanceTerraHeight = "+" + DistanceTerraHeight.PadLeft(5, "0")
                    End If
                    '温度0.1摄氏度，4位，缺失写入-999
                    If Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature = Nothing Then
                        '-0
                        DryTemperature1 = -999
                        ' DryTemperature = DryTemperature1.PadLeft(4)
                    ElseIf Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature = "-9990" Then
                        Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature = "-999"
                    Else
                        If Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature.ToString.StartsWith("-") Then
                            DryTemperature = Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature '.PadLeft(7)
                            DryTemperature = Mid(DryTemperature, 2, DryTemperature.Length - 1)
                            DryTemperature = DryTemperature.PadLeft(3, "0")
                            DryTemperature = "-" & DryTemperature
                        Else
                            DryTemperature = Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature '.PadLeft(7)
                            DryTemperature = DryTemperature.PadLeft(3, "0")
                            DryTemperature = "+" & DryTemperature
                        End If

                    End If

                    '计算相对湿度，小阳改
                    Dim rh As Double = Dewpoint_RH(Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DryTemperature / 10, Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DewPointTemperature / 10)

                    sngRH = Format(rh, "0")

                    sngRH = sngRH.PadLeft(3, "0")
                    'If Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DewPointTemperature = Nothing Then
                    '    '- - - 0
                    '    DewPointTemperature1 = 999
                    '    DewPointTemperature = DewPointTemperature1.PadLeft(3)
                    'Else
                    '    DewPointTemperature = Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).DewPointTemperature '.PadLeft(7)
                    '    DewPointTemperature = Mid(DewPointTemperature, 1, 3) ' DewPointTemperature.PadLeft(3)
                    'End If
                    '风向，单位度，缺失写入999	，3位
                    If Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindVane = Nothing Then
                        WindVane1 = 999
                    Else
                        WindVane = Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindVane
                        WindVane = WindVane.PadLeft(3, "0") 'Mid(WindVane, 1, 3) '
                    End If
                    '风速，单位m/s，缺失写入999	，3位
                    If Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindSpeed = Nothing Then
                        '-0
                        WindSpeed1 = 999
                    ElseIf Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindSpeed = 9990 Then
                        WindSpeed1 = 999

                    Else
                        '小阳改动
                        ' WindSpeed = Me.m_reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindSpeed
                        intwindspeed = CInt(Me.reconnaissance.ArrReconnaissance(f1).ArrtierData(f2).WindSpeed)
                        intwindspeed = intwindspeed / 10 ' * 10
                        WindSpeed = CSng(intwindspeed)
                        WindSpeed = WindSpeed.PadLeft(3, "0") 'Mid(WindSpeed, 1, 3)
                    End If


                    'str += pa
                    'str += DistanceTerraHeight
                    'str += DryTemperature
                    'str += DewPointTemperature
                    'str += WindVane
                    'str += WindSpeed

                    'myear = myear & Me.m_reconnaissance.ArrReconnaissance(f1).Month & Me.m_reconnaissance.ArrReconnaissance(f1).Day & Me.m_reconnaissance.ArrReconnaissance(f1).Time
                    'str = Me.m_title.WeatherID.PadLeft(8, "0") & Lat.ToString.PadLeft(4, "0") & Me.m_title.Latitude.Remove(0, Me.m_title.Latitude.Length - 1) _
                    '& Lon.ToString.PadLeft(4, "0") & Me.m_title.Longitude.Remove(0, Me.m_title.Longitude.Length - 1) & myear & Me.m_reconnaissance.ArrReconnaissance(f1).TierCount.ToString.PadLeft(3, "0") & "0" & "9"
                    '写入每层数据信息
                    str = TIME_SINCE_RELEASE.PadLeft(5, "0") '气球经过时间，小阳改
                    str += pa '气压
                    str += DistanceTerraHeight '位势高度
                    str += DryTemperature '干球温度
                    str += sngRH ' "0" & sngRH '"070" ' 相对湿度，小阳改。
                    str += WindVane '风向
                    str += WindSpeed '风速
                    str += "000000" & "1" '每层的数据标志 calpuff 为“999999”+“9” aermod 为“000000”+“1”
                    swr.Write(str)
                Next
                swr.Write(vbCrLf)
            End If


        Next
        swr.Close()
        swr.Dispose()
    End Sub
    ''' <summary>
    ''' 干球温度、露点温度转为相对湿度，%
    ''' </summary>
    ''' <param name="Drytemp">干球温度</param>
    ''' <param name="Dewpoint">露点温度</param>
    ''' <returns>相对湿度 %</returns>
    ''' <remarks></remarks>
    Private Function Dewpoint_RH(ByVal Drytemp As Double, ByVal Dewpoint As Double) As Double
        Dim dblRH As Double
        Dim k As Double = 7.65 * Drytemp / (243.12 + Drytemp)

        Dim c As Double = (7.65 * Dewpoint - k * Dewpoint - 243.12 * k) / (243.12 - Dewpoint)

        dblRH = 10 ^ (c + 2)



        'Dim b1 As Double = (7.65 - k - 2) * Dewpoint + 243.12 * 2

        'Dim b2 As Double = b1 / (243.12 + Dewpoint)

        'dblRH = 10 ^ b2

        'Dim a1 As Double = 7.65 * Dewpoint
        'Dim a2 As Double = k * Dewpoint
        'Dim a3 As Double = 2 * Dewpoint
        'Dim a4 As Double = 2 * 243.12


        'Dim a5 As Double = a1 - a2 - a3 + a4

        'Dim a6 As Double = a5 / (243.12 + Dewpoint)

        'dblRH = 10 ^ a6
        Dewpoint_RH = CInt(dblRH)

    End Function
    ''' <summary>
    ''' 获取路径字符串中 文件的名字
    ''' </summary>
    ''' <param name="sName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetFileName(ByRef sName As String) As String
        Dim nStart As Short
        Dim nEnd As Short
        Dim nLen As Short
        nStart = 1
        nEnd = 1
        nLen = Len(sName)
        While nEnd <> 0
            nEnd = InStr(nStart, sName, "\")
            If nEnd <> 0 Then
                nStart = nEnd + 1
            End If
        End While
        GetFileName = Mid(sName, nStart, nLen - 4 - nStart + 1)
    End Function
End Class
